package com.ct.gd.passport.view.widget;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.ct.gd.passport.R;
import com.ct.gd.passport.common.CTApp;
import com.up91.common.android.widget.header.HeaderFragment;

public class CustomHeaderFragment extends HeaderFragment {
	private TextView	mTVCenterTitle;
	private TextView	mTVRight;
	private int			mMinHeight	= (int) convertDpToPixel(44, CTApp.getInstance());

	// temp
	public static float convertDpToPixel(float dp, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float px = dp * (metrics.densityDpi / 160f);
		return px;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ViewGroup v = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState);

		DefaultHeaderViewFactory headerViewFactory = new DefaultHeaderViewFactory(inflater);
		// View defaultLeftView =
		// headerViewFactory.getBackLeftView(getActivity());
		// setLeftView(defaultLeftView);
		setDefaultLeftView();

		View titleCenterView = headerViewFactory.getTitleCenterView();
		mTVCenterTitle = (TextView) titleCenterView.findViewById(R.id.tv_header_center);
		setCenterView(titleCenterView);

//		View defaultRightView = headerViewFactory.getRightView();
//		mTVRight = (TextView) defaultRightView.findViewById(R.id.tv_header_right);
//		setRightView(defaultRightView);
		setDefaultRightView();

		v.setMinimumHeight(mMinHeight);
		v.setBackgroundResource(R.color.bg_header);
		return v;
	}

	private void setDefaultLeftView() {
		setLeftView(R.layout.customheader_left);
		ImageButton btnLeft = (ImageButton) getLeftRoot().findViewById(R.id.btn_header_left);
		btnLeft.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				getActivity().finish();
			}
		});
		getLeftRoot().getChildAt(0).setMinimumHeight(mMinHeight);
	}
	
	private void setDefaultRightView() {
		setRightView(R.layout.customheader_right);
		mTVRight = (TextView) getRightRoot().findViewById(R.id.tv_header_right);
		getRightRoot().getChildAt(0).setMinimumHeight(mMinHeight);
		getRightRoot().getChildAt(0).setMinimumWidth(mMinHeight);
	}

	public void setBackground(int resId) {
		getView().setBackgroundResource(resId);
	}

	public void setLeftView(int resId) {
		getLeftRoot().removeAllViews();
		getLayoutInflater(null).inflate(resId, getLeftRoot());
	}

	public void setLeftView(View view) {
		getLeftRoot().removeAllViews();
		if (null != view) getLeftRoot().addView(view);
	}

	public void setRightView(int resId) {
		getRightRoot().removeAllViews();
		getLayoutInflater(null).inflate(resId, getRightRoot());
	}
	
	public void setRightView(View view) {
		getRightRoot().removeAllViews();
		if (null != view) getRightRoot().addView(view);
	}

	public View getLeftView() {
		return getLeftRoot().getChildAt(0);
	}

	public View getRightView() {
		return getRightRoot().getChildAt(0);
	}

	public void setCenterView(View view) {
		getCenterRoot().removeAllViews();
		if (null != view) getCenterRoot().addView(view);
	}

	public View getCenterView() {
		return getCenterRoot().getChildAt(0);
	}

	public void setCenterText(CharSequence text) {
		mTVCenterTitle.setText(text);
	}

	public void setCenterText(int resId) {
		mTVCenterTitle.setText(resId);
	}

	public void setRightText(CharSequence text) {
		mTVRight.setText(text);
	}

	public void setRightText(int resId) {
		mTVRight.setText(resId);
	}

	public void setRightOnClickListener(OnClickListener listener) {
		mTVRight.setOnClickListener(listener);
	}

}
